package cn.com.thingo.hyintelligent.ui.activity;


import android.annotation.SuppressLint;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.th.opensdk.openapi.NotificationInfo;
import com.th.opensdk.openapi.THOpenSDK;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.app.App;
import cn.com.thingo.hyintelligent.base.BaseActivity;
import cn.com.thingo.hyintelligent.base.contract.AddGatewayContract;
import cn.com.thingo.hyintelligent.model.bean.CommonBean;
import cn.com.thingo.hyintelligent.presenter.AddGatewayPresenter;
import cn.com.thingo.hyintelligent.ui.receiver.THOpenReceiver;
import cn.com.thingo.hyintelligent.util.SPUtils;
import cn.com.thingo.hyintelligent.util.ToastUtil;

/**
 * 添加网关
 */
public class AddGatewayActivity extends BaseActivity<AddGatewayPresenter> implements AddGatewayContract.View, THOpenReceiver.CallBack {
    @BindView(R.id.toolbar_left)
    ImageView tvToolbarLeft;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.add_gateway_device_num)
    EditText etDeviceNum;
    @BindView(R.id.add_gateway_account)
    EditText etUsername;
    @BindView(R.id.add_gateway_password)
    EditText etPassword;
    @BindView(R.id.add_gateway_name)
    EditText etDeviceName;
    @BindView(R.id.add_hint)
    View progress;
    private THOpenReceiver receiver;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    gatewayConnectFail();
                    App.getInstance().setGatewayLoginStatus(true);
                    break;
            }
        }
    };
    private String deviceNum;
    private String username;
    private String password;
    private String deviceName;


    @Override
    protected int getLayout() {
        return R.layout.activity_add_gateway;
    }

    @Override
    protected void initEventAndData() {
        initView();
    }

    private void initView() {
        tvToolbarLeft.setImageResource(R.mipmap.back);
        tvTitle.setText(R.string.add_gateway);
        initReceiver();
    }

    private void initReceiver() {
        receiver = new THOpenReceiver(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(NotificationInfo.BROADCAST_GATEWAY_STATUS_CHANGED);
        registerReceiver(receiver, filter);

    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }


    @OnClick({R.id.add_gateway_login_use, R.id.toolbar_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_gateway_login_use:
                deviceName = etDeviceName.getText().toString().trim();
                deviceNum = etDeviceNum.getText().toString().trim();
                username = etUsername.getText().toString().trim();
                password = etPassword.getText().toString().trim();
                if (TextUtils.isEmpty(deviceName)) {
                    ToastUtil.shortShow("设备名不能为空！");
                } else if (TextUtils.isEmpty(deviceNum)) {
                    ToastUtil.shortShow("设备号不能为空！");
                } else if (TextUtils.isEmpty(username)) {
                    ToastUtil.shortShow("用户名不能为空！");
                } else if (TextUtils.isEmpty(password)) {
                    ToastUtil.shortShow("密码不能为空！");
                } else {
                    progress.setVisibility(View.VISIBLE);
                    new Thread() {
                        @Override
                        public void run() {
                            int i = THOpenSDK.getInstance().loginGateway(deviceNum, username, password);
                            if (i != 0) {
                                handler.sendEmptyMessage(1);
                            }
                        }
                    }.start();

                }
                break;
            case R.id.toolbar_left:
                finish();
                break;
        }

    }

    //添加网关成功的回调
    @Override
    public void viewAddGateway(CommonBean commonBean) {
        ToastUtil.shortShow("网关添加成功！");
        progress.setVisibility(View.GONE);
        finish();
    }


    //隐藏登录的进度条
    public void hideProgress() {
        progress.setVisibility(View.GONE);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    public void gatewayConnectFail() {
        ToastUtil.shortShow("网关无法连接！");
        hideProgress();
    }

    //网关登录成功的广播回调
    @Override
    public void callBack() {
        hideProgress();
        THOpenSDK.getInstance().logoutGateway();
        mPresenter.addGateway(SPUtils.newInstance().getString(SPUtils.LOGIN_TOKEN), deviceNum, deviceName, username, password);
    }
}
